<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;
use think\Hook;

/**
 * 手機短信接口
 */
class Sms extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 發送驗證碼
     *
     * @ApiMethod (POST)
     * @param string $mobile 手機號
     * @param string $event 事件名稱
     */
    public function send()
    {
        $mobile = $this->request->post("mobile");
        $event = $this->request->post("event");
        $countryCode = $this->request->post("countryCode",'86');
        // 事件 mobilelogin 登錄 changemobile 修改手機號碼 verificationmobile 验证原手机
        $event = $event ? $event : 'mobilelogin';

        // || !\think\Validate::regex($mobile, "^1\d{10}$")
        if (!$mobile && $event != 'verificationmobile') {
            $this->error('請填寫手機號碼');
        }

        // $last = Smslib::get($mobile, $event);
        // if ($last && time() - $last['createtime'] < 60) {
        //     $this->error(__('發送頻繁'));
        // }
        // $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        // if ($ipSendTotal >= 5) {
        //     $this->error(__('發送頻繁'));
        // }
        if ($event) {
            if ( $event == 'verificationmobile' ) {
                $mobile = $this->auth->mobile;
            }
            $userinfo = User::getByMobile($mobile);
            if ( $event == 'changemobile' && !$this->auth->id ) {
                $this->error('請重新登錄',[],100);
            }
            if ( $event == 'verificationmobile' && !$this->auth->id ) {
                $this->error('請重新登錄',[],100);
            }
            if ($event == 'changemobile' && $userinfo) {
                //已被註冊
                $this->error('手機號碼已被佔用');
            }
        }

        $type = 1;
        if ( $countryCode && $countryCode != '86' ) {
            $type = 2;
        }
        $ret = Smslib::send($mobile, null, $event,$type,$countryCode);
        // $ret = true;
        if ($ret) {
            $this->success(__('發送成功'));
        } else {
            $this->error(__('發送失敗，請檢查短信配置是否正確'));
        }
    }

    /**
     * 檢測驗證碼
     *
     * @ApiMethod (POST)
     * @param string $mobile 手機號
     * @param string $event 事件名稱
     * @param string $captcha 驗證碼
     */
    public function check()
    {
        $mobile = $this->request->post("mobile");
        $event = $this->request->post("event");
        $event = $event ? $event : 'register';
        $captcha = $this->request->post("captcha");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手機號不正確'));
        }
        if ($event) {
            $userinfo = User::getByMobile($mobile);
            if ($event == 'register' && $userinfo) {
                //已被註冊
                $this->error(__('已被註冊'));
            } elseif (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                $this->error(__('已被占用'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未註冊
                $this->error(__('未註冊'));
            }
        }
        $ret = Smslib::check($mobile, $captcha, $event);
        if ($ret) {
            $this->success(__('成功'));
        } else {
            $this->error(__('驗證碼不正確'));
        }
    }
}
